package neo.app.service;

import java.util.Map;

import neo.core.common.PagingList;
import neo.core.util.MapUtil;

public class RoleService extends BaseService {

	//角色列表
	private static final String SQL_GET_ALL_ROLES = "select role_id,role_name,role_desc,IFNULL(a.user_num,0) as user_num from ken_role left join (select urole_role_id,count(*) as user_num from ken_urole group by urole_role_id) a on role_id = a.urole_role_id";
	
	/**
	 * 角色列表
	 * @return
	 */
	public PagingList getAllRoles() {
		return getPagingList(SQL_GET_ALL_ROLES);
	}
	
	//添加角色
	private static final String SQL_INSERT_ROLE = "insert into ken_role values(role_id,?,?)";
	/**
	 * 添加角色
	 * @param map
	 * @return
	 */
	public boolean addRole(Map map) {
		//1、获取值
		String role_name = MapUtil.getStringFromMap(map, "role_name");
		String role_desc = MapUtil.getStringFromMap(map, "role_desc");
		//2、校验数据有效性
		
		if(jt.update(SQL_INSERT_ROLE, new Object[]{role_name, role_desc }) > 0){
			return true;
		}
		return false;
	}
	
	//查看角色下的用户
	private static final String SQL_GET_USERS_BY_ROLE_ID = "select distinct(user_id),employee_no,employee_name from ken_urole join ken_user on user_id=urole_user_id join ken_employee on user_employee_id=employee_id where urole_role_id=?";
	/**
	 * 查看角色下的用户
	 */
	public PagingList getUsersByRoleId(Map map) {
		String role_id = MapUtil.getStringFromMap(map, "role_id");
		return getPagingList(SQL_GET_USERS_BY_ROLE_ID, new Object[]{role_id });
	}

}
